<template>
  <div>
    <el-card>
      <div slot="header">
        <span>订单管理</span>
      </div>
      <div class="content">
        <!-- 查询表单 -->
        <el-form size="samll" :inline="true" :model="searchForm">
          <el-form-item label="订单号">
            <el-input v-model="searchForm.orderNo"></el-input>
          </el-form-item>
          <el-form-item label="收货人">
            <el-input v-model="searchForm.consignee"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="searchForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="订单状态">
            <el-select v-model="searchForm.orderState">
              <el-option value="已受理">已受理</el-option>
              <el-option value="派送中">派送中</el-option>
              <el-option value="已完成">已完成</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="下单时间">
            <el-date-picker format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" v-model="searchForm.date" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"> </el-date-picker>
          </el-form-item>
          <el-form-item label="">
            <el-button type="primary" @click="search">查询</el-button>
            <el-button type="primary" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 订单表格 -->
        <el-table :data="tableData" border :style="{ width: w + 'px' }">
          <!-- 订单号 -->
          <el-table-column fixed prop="orderNo" label="订单号" width="80"> </el-table-column>
          <!-- 下单时间 -->
          <el-table-column label="下单时间" width="180">
            <template slot-scope="scope">
              {{ scope.row.orderTime | formatDatenyrsfm }}
            </template>
          </el-table-column>
          <!-- 手机号 -->
          <el-table-column prop="phone" label="手机号" width="120"> </el-table-column>
          <!-- 收货人 -->
          <el-table-column prop="consignee" label="收货人" width="120"> </el-table-column>
          <!-- 配送地址 -->
          <el-table-column prop="deliverAddress" label="配送地址" width="120"> </el-table-column>
          <!-- 送达时间 -->
          <el-table-column prop="deliveryTime" label="送达时间" width="120"> </el-table-column>
          <!-- 用户备注 -->
          <el-table-column prop="remarks" label="用户备注" width="120"> </el-table-column>
          <!-- 订单金额 -->
          <el-table-column prop="orderAmount" label="订单金额" width="120"> </el-table-column>
          <!-- 订单状态 -->
          <el-table-column prop="orderState" label="订单状态" width="120"> </el-table-column>
          <!-- 操作 -->
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
              <el-button @click="handleEdit" type="text" size="small">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination @current-change="currentChange" layout="total,prev, pager, next,jumper" :total="total" :current-page="currentPage" :page-size="pageSize"> </el-pagination>
      </div>

      <!-- 订单编辑 -->
      <el-drawer title="订单编辑" :visible.sync="drawer" :direction="direction" :before-close="handleClose">
        <span>没写编辑哦没写编辑哦没写编辑哦没写编辑哦没写编辑哦没写编辑哦</span>
      </el-drawer>
    </el-card>
  </div>
</template>

<script>
import { getOrderList } from '@/api/order'
export default {
  data() {
    return {
      // 表格动态样式
      w: document.body.clientWidth - 300,
      drawer: false,
      direction: 'rtl',
      searchForm: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: []
      },
      // 表格数据
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0
    }
  },
  created() {
    window.addEventListener('resize', this.resize)
    this.getData()
  },
  beforeDestroy() {
    // 组件销毁前移除监听
    window.removeEventListener('resize', this.resize)
  },
  methods: {
    resize() {
      this.w = document.body.clientWidth - 300
    },
    async getData(searchParams = {}) {
      let res = await getOrderList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ...searchParams
      })
      let { total, data } = res.data
      this.total = total
      this.tableData = data
    },
    currentChange(val) {
      this.currentPage = val
      this.getData(this.searchParams)
    },
    // 查询
    search() {
      this.currentPage = 1
      this.searchParams = JSON.parse(JSON.stringify(this.searchForm))
      this.searchParams.date = JSON.stringify(this.searchParams.date)
      this.getData(this.searchParams)
    },
    // 重置
    resetForm() {
      this.searchForm = {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: []
      }
      this.search()
    },
    // 查看
    handleClick() {
      this.$message({
        type: 'warning',
        message: '别看了，我没写!!!'
      })
    },
    // 编辑订单
    handleEdit() {
      this.drawer = true
    },
    // 编辑订单完成
    handleClose() {
      this.drawer = false
      console.log('done')
    }
  }
}
</script>

<style></style>
